﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using PocketMahjong.Core;
using System.Reflection;

namespace PocketMahjong
{
	static class LayoutProvider
	{
		private static Dictionary<string, Layout> _layouts;
		public static IEnumerable<Layout> Layouts
		{
			get
			{
				if (_layouts == null)
					Load();
				return _layouts.Values.OrderBy(p => p.Name).ToArray();
			}
		}

		private static void Load()
		{
			var w = new System.Diagnostics.Stopwatch();
			w.Start();

			var prefix = "PocketMahjong.Layouts.";
			_layouts = new Dictionary<string, Layout>();
			Assembly asm = Assembly.GetExecutingAssembly();
			foreach (string name in asm.GetManifestResourceNames())
				if (name.StartsWith(prefix))
				{
					var data = asm.GetManifestResourceStream(name);
					var lay = Layout.Load(data);
					lay.Name = name.Replace(prefix, "").Replace(".bin","");
					_layouts.Add(lay.Name.ToLower(), lay);
				}

			w.Stop();
			var t =  w.ElapsedMilliseconds;
		}

		public static Layout GetLayout(string name)
		{
			if (_layouts == null)
				Load();

			name = name.ToLower();
			if (_layouts.ContainsKey(name))
				return _layouts[name];
			else
				throw new ArgumentOutOfRangeException("name");
		}
	}
}
